package com.xiaoka.ins.corp.api.aics.common;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.xiaoka.ins.corp.api.aics.dto.config.Risk;
import com.xiaoka.ins.corp.api.aics.dto.config.StaticConfig;
import com.xiaoka.ins.corp.api.aics.wsclient.CoverageData;
import com.xiaoka.ins.corp.api.aics.wsclient.RequestCvrg;
import com.xiaoka.ins.corp.api.dto.request.RiskDto;
import com.xiaoka.ins.corp.api.dto.request.VehicleDto;
import com.xiaoka.ins.corp.api.utils.BaseUtils;

/**
 * 险别转换
 * @author donggua 
 */
public class RiskTransfer {

	/**
	 * 转成保险公司商业险险别
	 * 
	 * @param vehicleDto
	 * @param risks
	 * @param glassType
	 * @return
	 */
	public static List<CoverageData> getBizRisk(VehicleDto vehicleDto,
			List<RiskDto> risks, String glassType) {
		List<CoverageData> coverageDatas = new ArrayList<CoverageData>();

		int index = 0;
		// 载入险别配置信息
		List<Risk> riskConfig = StaticConfig.risks;
		// 主险不计免赔
		List<Risk> mainRiskDto = riskConfig.stream()
				.filter(p -> p.getCode().startsWith("NM"))
				.collect(Collectors.toList());
		// 附加险不计免赔
		List<Risk> addRiskDto = riskConfig.stream()
				.filter(p -> p.getCode().startsWith("NA"))
				.collect(Collectors.toList());
		if (mainRiskDto.size() > 0) {
			index++;
			CoverageData coverageData = new CoverageData();
			coverageData.setCoverageSeq(index + "");
			coverageData.setCoverageCode("030194");
			coverageData.setCoverageFranchise("1");
			coverageData.setFranchiseFlag("");
			coverageData.setCoverageAmount("");
			coverageData.setCoverageAmountThird("");
			coverageData.setCoverageNumber("");
			coverageData.setCoverageRate("");
			coverageData.setCoveragePremium("");
			coverageData.setBasicPremium("");
			coverageData.setBeforeDiscountPremium("");
			coverageData.setBeforeDiscountYearPremium("");
			coverageData.setAfterDiscountYearPremium("");
			coverageData.setCoverageRemark("");
			coverageData.setOriginalPremium("");
			coverageDatas.add(coverageData);
		}
		if (addRiskDto.size() > 0) {
			index++;
			CoverageData coverageData = new CoverageData();
			coverageData.setCoverageSeq(index + "");
			coverageData.setCoverageCode("030195");
			coverageData.setCoverageFranchise("1");
			coverageData.setFranchiseFlag("");
			coverageData.setCoverageAmount("");
			coverageData.setCoverageAmountThird("");
			coverageData.setCoverageNumber("");
			coverageData.setCoverageRate("");
			coverageData.setCoveragePremium("");
			coverageData.setBasicPremium("");
			coverageData.setBeforeDiscountPremium("");
			coverageData.setBeforeDiscountYearPremium("");
			coverageData.setAfterDiscountYearPremium("");
			coverageData.setCoverageRemark("");
			coverageData.setOriginalPremium("");
			coverageDatas.add(coverageData);
		}
		String amount = "", franchise, coverageNum;
		Risk riskFirst;
		// 玻璃类型 国产:303011001 进口:303011002
		glassType = "303011001";

		// 遍历点点车险险种标准
		for (int i = 0; i < risks.size(); i++) {
			index++;
			RiskDto risk = risks.get(i);
			// 因永诚不计免赔不作为险种所以跳出
			if (risk.getCode().startsWith("N")) {
				continue;
			}
			// 映射对象
			riskFirst = riskConfig.stream()
					.filter(p -> p.getCode().equals(risk.getCode()))
					.findFirst().get();
			// 保额
			amount = riskFirst.isUsePurchaseTag() ? vehicleDto
					.getPurchasePriceIncludeTax() : risk.getValue().toString();
			// 如果有勾选玻璃险
			if (risk.getCode() == "A01"
					&& BaseUtils.deleteDecimalPoint(risk.getValue() + "")
							.equals("1")) {
				glassType = "303011002";
			}
			coverageNum = "";
			String riskCode = riskFirst.getCode();
			if (riskCode.startsWith("M")) {
				franchise = mainRiskDto.stream()
						.filter(p -> p.getCode().contains(riskCode)).count() > 0 ? "1"
						: "0";

				if (riskFirst.getCode() == "M05") {
					coverageNum = "1";
				}
				if (riskFirst.getCode() == "M06") {
					coverageNum = (vehicleDto.getVehicleSeat() - 1) + "";
				}
			} else {
				franchise = addRiskDto.stream()
						.filter(p -> p.getCode().contains(riskCode)).count() > 0 ? "1"
						: "0";
			}

			CoverageData coverageData = new CoverageData();
			coverageData.setCoverageSeq(index + "");
			coverageData.setCoverageCode(riskFirst.getvCode());
			coverageData.setCoverageFranchise(franchise);
			coverageData.setCoverageNumber(coverageNum);
			coverageData.setCoverageAmount(amount);
			coverageData.setFranchiseFlag("");
			coverageData.setCoverageAmountThird("");
			coverageData.setCoverageRate("");
			coverageData.setCoveragePremium("");
			coverageData.setBasicPremium("");
			coverageData.setBeforeDiscountPremium("");
			coverageData.setBeforeDiscountYearPremium("");
			coverageData.setAfterDiscountYearPremium("");
			coverageData.setCoverageRemark("");
			coverageData.setOriginalPremium("");
			coverageDatas.add(coverageData);
		}
		return coverageDatas;
	}

	/**
	 * 转成保险公司费改商业险险别
	 * 
	 * @param vehicleDto
	 * @param risks
	 * @param glassType
	 * @return
	 */
	public static List<RequestCvrg> getNewBizRisk(VehicleDto vehicleDto,
			List<RiskDto> risks, String glassType) {
		List<RequestCvrg> coverageDatas = new ArrayList<RequestCvrg>();

		// 载入险别配置信息
		List<Risk> riskConfig = StaticConfig.risks;

		String amount = "", riskCode, franchise, coverageNum;
		Risk riskFirst;
		// 玻璃类型 国产:303011001 进口:303011002
		glassType = "303011001";

		// 遍历点点车险险种标准
		for (int i = 0; i < risks.size(); i++) {
			RiskDto risk = risks.get(i);
			// 因永诚不计免赔不作为险种所以跳出
			if (risk.getCode().startsWith("N")) {
				continue;
			}
			// 映射对象
			riskFirst = riskConfig.stream()
					.filter(p -> p.getCode().equals(risk.getCode()))
					.findFirst().get();
			// 保额
			amount = riskFirst.isUsePurchaseTag() ? vehicleDto
					.getPurchasePriceIncludeTax() : risk.getValue().toString();
			// 如果有勾选玻璃险
			if (risk.getCode() == "A01") {
				glassType = BaseUtils.deleteDecimalPoint(risk.getValue() + "")
						.equals("1") ? "303011002" : "303011001";
			}
			coverageNum = "";
			riskCode = riskFirst.getCode();
			if (riskCode.startsWith("M")) {
				if (riskFirst.getCode() == "M05") {
					coverageNum = "1";
				}
				if (riskFirst.getCode() == "M06") {
					coverageNum = (vehicleDto.getVehicleSeat() - 1) + "";
				}
			}
			franchise = risks.stream()
					.filter(p -> p.getCode().equals("N" + risk.getCode()))
					.findFirst() == null ? "0" : "1";
			
			RequestCvrg coverageData = new RequestCvrg();
			//险种代码
			coverageData.setCCvrgNo(riskFirst.getvCode());
			//不计免赔 1 投 0 不投
			coverageData.setCDductMrk(franchise);
			//单位保额
			coverageData.setNPerAmt(amount);
			//玻璃类型
			coverageData.setCGlassTyp(glassType);
			//指定修理厂险国产,进口
			coverageData.setCResvTxt1("");
			//车损免赔额
			coverageData.setCResvTxt2("341003001");
			 //单位（责任天数/人数）
			 //1，当险别为【030151修理期间费用补偿险】时，该字段表征为【天数】；
			 //2，当险别为【030102机动车车上人员责任保险(乘客)】时，该字段表征为投保该险别的乘客数。
			 //备注：投保以上两险别时该字段必传且不能为０。其他险别不传。
			coverageData.setNLiabDaysLmt(coverageNum);
			 //指定修理厂浮动比例 当险别为【030202指定修理厂险】时，该字段必传。如果浮动比例为15%传15。 进口（15%到60%）
			 //国产（10%到30%）
			coverageData.setNSepcRep("");
			coverageDatas.add(coverageData);
		}
		return coverageDatas;
	}
//	public static List<com.xiaoka.ins.corp.api.aics.stub.RequestCvrg> getNewBizRisk(VehicleDto vehicleDto,
//			List<RiskDto> risks, String glassType) {
//		List<com.xiaoka.ins.corp.api.aics.stub.RequestCvrg> coverageDatas = new ArrayList<com.xiaoka.ins.corp.api.aics.stub.RequestCvrg>();
//
//		// 载入险别配置信息
//		List<Risk> riskConfig = StaticConfig.risks;
//
//		String amount = "", riskCode, franchise, coverageNum;
//		Risk riskFirst;
//		// 玻璃类型 国产:303011001 进口:303011002
//		glassType = "303011001";
//
//		// 遍历点点车险险种标准
//		for (int i = 0; i < risks.size(); i++) {
//			RiskDto risk = risks.get(i);
//			// 因永诚不计免赔不作为险种所以跳出
//			if (risk.getCode().startsWith("N")) {
//				continue;
//			}
//			// 映射对象
//			riskFirst = riskConfig.stream()
//					.filter(p -> p.getCode().equals(risk.getCode()))
//					.findFirst().get();
//			// 保额
//			amount = riskFirst.isUsePurchaseTag() ? vehicleDto
//					.getPurchasePriceIncludeTax() : risk.getValue().toString();
//			// 如果有勾选玻璃险
//			if (risk.getCode() == "A01") {
//				glassType = BaseUtils.deleteDecimalPoint(risk.getValue() + "")
//						.equals("1") ? "303011002" : "303011001";
//			}
//			coverageNum = "";
//			riskCode = riskFirst.getCode();
//			if (riskCode.startsWith("M")) {
//				if (riskFirst.getCode() == "M05") {
//					coverageNum = "1";
//				}
//				if (riskFirst.getCode() == "M06") {
//					coverageNum = (vehicleDto.getVehicleSeat() - 1) + "";
//				}
//			}
//			franchise = risks.stream()
//					.filter(p -> p.getCode().equals("N" + risk.getCode()))
//					.findFirst() == null ? "0" : "1";
//			
//			com.xiaoka.ins.corp.api.aics.stub.RequestCvrg coverageData = new com.xiaoka.ins.corp.api.aics.stub.RequestCvrg();
//			//险种代码
//			coverageData.setCCvrgNo(riskFirst.getvCode());
//			//不计免赔 1 投 0 不投
//			coverageData.setCDductMrk(franchise);
//			//单位保额
//			coverageData.setNPerAmt(amount);
//			//玻璃类型
//			coverageData.setCGlassTyp(glassType);
//			//指定修理厂险国产,进口
//			coverageData.setCResvTxt1("");
//			//车损免赔额
//			coverageData.setCResvTxt2("341003001");
//			 //单位（责任天数/人数）
//			 //1，当险别为【030151修理期间费用补偿险】时，该字段表征为【天数】；
//			 //2，当险别为【030102机动车车上人员责任保险(乘客)】时，该字段表征为投保该险别的乘客数。
//			 //备注：投保以上两险别时该字段必传且不能为０。其他险别不传。
//			coverageData.setNLiabDaysLmt(coverageNum);
//			 //指定修理厂浮动比例 当险别为【030202指定修理厂险】时，该字段必传。如果浮动比例为15%传15。 进口（15%到60%）
//			 //国产（10%到30%）
//			coverageData.setNSepcRep("");
//			coverageDatas.add(coverageData);
//		}
//		return coverageDatas;
//	}

	/**
	 * 获取保险公司交强险险别
	 * 
	 * @return
	 */
	public static List<CoverageData> getTrafficRisk() {
		List<CoverageData> coverageDatas = new ArrayList<CoverageData>();
		CoverageData coverageData = new CoverageData();
		coverageData.setCoverageSeq("1");
		coverageData.setCoverageCode("030050");
		//此处需要填写默认保额
		coverageData.setCoverageAmount("122000");
		coverageData.setCoverageFranchise("");
		coverageData.setFranchiseFlag("");
		coverageData.setCoverageAmountThird("");
		coverageData.setCoverageNumber("");
		coverageData.setCoverageRate("");
		coverageData.setCoveragePremium("");
		coverageData.setBasicPremium("");
		coverageData.setBeforeDiscountPremium("");
		coverageData.setBeforeDiscountYearPremium("");
		coverageData.setAfterDiscountYearPremium("");
		coverageData.setCoverageRemark("");
		coverageData.setOriginalPremium("");
		coverageDatas.add(coverageData);
		return coverageDatas;
	}
}
